#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author  : lei.wang
import os
import uuid
from django.conf import settings


class ImageHelper:

    @staticmethod
    def save_chanllenge_image(file, chanllenge_uuid, wei_chat_open_id):
        media_path = settings.MEDIA_ROOT
        if not os.path.exists(os.path.join(media_path, 'chanllenge')):
            os.makedirs(os.path.join(media_path, 'chanllenge'), exist_ok=True)

        origin_filename, file_extension = os.path.splitext(file.name)
        new_filename = f'chanllenge_action_{chanllenge_uuid}_{wei_chat_open_id}_{uuid.uuid4()}{file_extension}'
        save_target = os.path.join(media_path, 'chanllenge', new_filename)
        with open(save_target, 'wb+') as f:
            for chunk in file.chunks():
                f.write(chunk)
        return f'media/chanllenge/{new_filename}'
